package cn.fresh.xuanyuan.module.infra.service.city;

import cn.fresh.xuanyuan.module.infra.dal.dataobject.city.CityDO;
import cn.fresh.xuanyuan.module.infra.dal.mysql.city.CityMapper;
import cn.fresh.xuanyuan.module.infra.vo.CityTreeVo;
import cn.fresh.xuanyuan.module.infra.vo.CityVo;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class CityServiceImpl implements CityService {

    @Resource
    private CityMapper cityMapper;

    @Override
    public List<CityTreeVo> getCityList() {
        List<CityVo> tree = new ArrayList<>();
        LambdaQueryWrapper<CityDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CityDO::getStatus, true);
        List<CityDO> list = cityMapper.selectList(queryWrapper);
        if(CollectionUtil.isNotEmpty(list)) {
            for(CityDO c : list) {
                if(c.getPid() == 0) {
                    CityVo v = new CityVo();
                    BeanUtils.copyProperties(c, v);
                    tree.add(v);
                }
            }

            for(CityVo p : tree) {
                List<CityVo> children = new ArrayList<>();
                for(CityDO c : list) {
                    if(c.getLevel() == 2 && c.getPid().equals(p.getId())) {
                        CityVo v = new CityVo();
                        BeanUtils.copyProperties(c, v);
                        List<CityVo> children1 = new ArrayList<>();
                        for(CityDO a : list) {
                            if(a.getLevel() == 3 && a.getPid().equals(c.getId())) {
                                CityVo v1 = new CityVo();
                                BeanUtils.copyProperties(a, v1);
                                children1.add(v1);
                            }
                        }
                        v.setChildren(children1);
                        children.add(v);
                    }
                }
                p.setChildren(children);
            }
        }
        List<CityTreeVo> treeData = new ArrayList<>();
        for(CityVo v : tree) {
            CityTreeVo a = getCityTreeVo(v);
            treeData.add(a);
        }

        return treeData;
    }

    @NotNull
    private static CityTreeVo getCityTreeVo(CityVo v) {
        CityTreeVo a = new CityTreeVo();
        a.setText(v.getName());
        a.setValue(v.getCode());
        List<CityTreeVo> children1 = new ArrayList<>();
        for(CityVo h : v.getChildren()) {
            CityTreeVo b = new CityTreeVo();
            b.setText(h.getName());
            b.setValue(h.getCode());
            List<CityTreeVo> children2 = new ArrayList<>();
            for(CityVo d : h.getChildren()) {
                CityTreeVo r = new CityTreeVo();
                r.setText(d.getName());
                r.setValue(d.getCode());
                children2.add(r);
            }
            b.setChildren(children2);
            children1.add(b);
        }
        a.setChildren(children1);
        return a;
    }
}
